package com.mostromas.ess.oauth2.service;

import com.mostromas.ess.entities.SysMenu;
import com.mostromas.ess.entities.SysUser;
import com.mostromas.ess.entities.UserProject;
import com.mostromas.ess.feign.IFeignProjectController;
import com.mostromas.ess.feign.IFeignSystemController;
import com.mostromas.ess.util.base.Result;
import com.mostromas.ess.util.enums.SysUserType;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired // 检查启动类注解 @EnableFeignClients
    private IFeignSystemController feignSystemController;

    @Autowired
    private IFeignProjectController feignProjectController;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 1. 判断用户名是否为空
        if(StringUtils.isEmpty(username)) {
            throw new BadCredentialsException("用户名不能为空");
        }
        // 2. 通过用户名查询数据库中的用户信息
        SysUser sysUser = feignSystemController.findUserByUsername(username);

        if(sysUser == null) {
            throw new BadCredentialsException("用户名不存在");
        }

        // 3. 通过用户id去查询数据库的拥有的权限信息
        List<SysMenu> menuList = feignSystemController.findMenuByUserId(sysUser.getId());

        // 4. 封装权限信息（权限标识符code）
        List<GrantedAuthority> authorities = null;
        if(CollectionUtils.isNotEmpty(menuList)) {
            authorities = new ArrayList<>();
            for(SysMenu menu: menuList) {
                if(menu != null){// 权限标识
                    String code = menu.getCode();
                    authorities.add(new SimpleGrantedAuthority(code));
                }

            }
        }
//        System.out.println("authorities========"+authorities);

        List<UserProject> projects = null;
        if(sysUser.getUserType() == SysUserType.PROJECT){
            projects = feignProjectController.getProjectsByUsername(sysUser.getUsername());
        }
        // 5. 构建UserDetails接口的实现类JwtUser对象
        JwtUser jwtUser = new JwtUser(
                sysUser.getId(), sysUser.getUsername(), sysUser.getPassword(),
                sysUser.getNickName(), sysUser.getImageUrl(), sysUser.getMobile(),
                sysUser.getEmail(),
                sysUser.getIsAccountNonExpired(), sysUser.getIsAccountNonLocked(),
                sysUser.getIsCredentialsNonExpired(), sysUser.getIsEnabled(),
                authorities,projects
        );
        return jwtUser;
    }
}
